function getRootPath(){  
    //获取当前网址，如： http://localhost:8083/uimcardprj/share/meun.jsp  
    var curWwwPath=window.document.location.href;  
    //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp  
    var pathName=window.document.location.pathname;  
    var pos=curWwwPath.indexOf(pathName);  
    //获取主机地址，如： http://localhost:8083  
    var localhostPaht=curWwwPath.substring(0,pos);  
    //获取带"/"的项目名，如：/uimcardprj  
    var projectName=pathName.substring(0,pathName.substr(1).indexOf('/')+1);  
    return(localhostPaht+projectName+"/");  
} 
var activityId;
var activityStatus = 0;

layui.config({
    base: getRootPath()+"common/larrylib/",
    version: '201708031'
}).use(['jquery', 'upload', 'common', 'form', 'laydate'], function () {
    var $ = layui.jquery
        , upload = layui.upload
        , form = layui.form
        , t = layui.layer
        , e = layui.common
        , laydate = layui.laydate;
    ;


    //开始时间
    laydate.render({
        elem: '#activityBeginTime' //指定元素
        , format: 'yyyy-MM-dd'
    });
    //活动结束时间
    laydate.render({
        elem: '#activityEndTime' //指定元素
        , format: 'yyyy-MM-dd'
    });

    //监听活动开关
    form.on('switch(activityStatus)', function (data) {
        layer.tips('温馨提示：ON呈现为绿色的时候为开启', data.othis)
        if (this.checked) {
            activityStatus = "1"
        } else {
            activityStatus = "0"
        }
    });
    //监听提交
    form.on("submit(create)", function (data) {

        var activityDiscountMoney = data.field.activityDiscountMoney;
        var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
        if (activityDiscountMoney == "" || !reg.test(activityDiscountMoney)) {
            top.layer.msg("请输入正确的金额！", {icon: 5});
            return false;
        }

        if (typeof(activityId) == "undefined" || activityId == null) {
            top.layer.msg("请选择图片！", {icon: 5});
            return false;
        }

        if (data.field.activityContent == "") {
            top.layer.msg("请输入活动内容！", {icon: 5});
            return false;
        }
        if (activityId <= 0) {
            top.layer.msg("banner图上传失败,请重试！", {icon: 5});
            return false;

        }
        var loginLoading = top.layer.msg('提交中，请稍候', {icon: 16, time: false, shade: 0.8});
        var reqdata = {
            activityId: activityId,
            activityTitle: data.field.activityTitle,
            activityContent: data.field.activityContent,
            activityStartTime: data.field.activityStartTime,
            activityEndTime: data.field.activityEndTime,
            activityStatus: activityStatus,
            activityDiscountMoney: data.field.activityDiscountMoney,
            activityDiscountNum: data.field.activityDiscountNum,
            activityURL: data.field.activityURL
        }
        $.ajax({
            url: getRootPath()+"agent/active/createActivity",
            type: 'post',
            async: false,
            data: JSON.stringify(reqdata),
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                if (data.resultCode == 1) {
                    top.layer.msg("创建成功！", {icon: 6});
                    return true;
                } else {
                    top.layer.msg(data.msg, {icon: 5});
                    return false;
                }
                top.layer.close(loginLoading);
            }, error: function () {
                top.layer.msg("系统异常！", {icon: 5});
                top.layer.close(loginLoading);
                return false;
            }
        });
    });

 
        //普通图片上传
        var uploadInst = upload.render({
            elem: '#zxSmallPic'
            , url: getRootPath()+"qiniu/uploadImg"
            // , auto: false //选择文件后不自动上传
            , field: "zxSmallPic"
            // , bindAction: '#create' //指向一个按钮触发上传
            // , field: "activityBanner"
            , before: function (obj) {
                //将每次选择的文件追加到文件队列
               // var files = obj.pushFile();
                //预读本地文件，如果是多文件，则会遍历。(不支持ie8/9)
                obj.preview(function (index, file, result) {
                   // $("#banner").css('display', 'block');
                    $('#showImg').attr('src', result); //图片链接（base64）

                    //obj.upload(index, file); //对上传失败的单个文件重新上传，一般在某个事件中使用
                    //delete files[index]; //删除列表中对应的文件，一般在某个事件中使用
                });
            }
            , done: function (data) {
                //如果上传失败
                if (data.state == "false") {
                    return layer.msg('上传失败');
                }
                imgUrl = data.res;
                $("#qiniu_zxSmallPic").val(imgUrl);
                $('#showImg').attr('src', imgUrl);
                //console.log(activityId)
                //上传成功
            }
            , error: function () {
                //演示失败状态，并实现重传
//                var demoText = $('#demoText');
//                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-mini demo-reload">重试</a>');
//                demoText.find('.demo-reload').on('click', function () {
//                    uploadInst.upload();
//                });
            	return layer.msg('上传失败');
            }
        });
});
